package ysu.sxm.ptjc.Service.Impl;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ysu.sxm.ptjc.Dao.TopStratisProsDao;
import ysu.sxm.ptjc.Domin.DateRange;
import ysu.sxm.ptjc.Domin.TopOfStratisProReq;
import ysu.sxm.ptjc.Domin.TopResp;
import ysu.sxm.ptjc.Service.TopStatisProsService;
import ysu.sxm.ptjc.Utill.Utills;
import ysu.sxm.ptjc.pojo.DefectTypeInfo;
import ysu.sxm.ptjc.pojo.ProduceStatisticalInfo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Service
public class TopStatisProsServiceImpl implements TopStatisProsService {
    @Autowired
    TopStratisProsDao topStratisProsDao;
    @Autowired
    DefectStatisticsServiceImpl defectStatisticsService;
    @Override
    public String topPros(TopOfStratisProReq straitsProReq) {
        List<DateRange> dateRanges=new ArrayList<>();
        if(straitsProReq.getDay()==null||straitsProReq.getDay().isEmpty()){
            if(straitsProReq.getWeek()==null||straitsProReq.getWeek().isEmpty()){
                if (straitsProReq.getMonth()!=null&&(!straitsProReq.getMonth().isEmpty())){
                    dateRanges= Utills.getMonthDays(straitsProReq.getMonth());
                }else{
                    return "没有数据!请选择你要查询的日期!";
                }
            }else{
                dateRanges=Utills.getWeekDays(straitsProReq.getWeek());
            }
        }else{
            for (String day:straitsProReq.getDay()
            ) {
                dateRanges.add(new DateRange(day,day));
            }
        }
        for (DateRange d: dateRanges
             ) {
            System.out.println("请求的日期为："+d.toString());
        }
        List<String> proNums=topStratisProsDao.getProNumsByCondition(straitsProReq,dateRanges);
        Map<String, TopResp> respMap=new LinkedHashMap<>();
        if (proNums!=null&&!proNums.isEmpty()){
            for (String s:proNums
                 ) {
                System.out.println("根据条件查询到的produce_num："+s);
            }
            List<ProduceStatisticalInfo> numToProNames=topStratisProsDao.getNumToProName(proNums);
            Map<String,List<String>> nameToNums=new LinkedHashMap<>();
            for (ProduceStatisticalInfo p: numToProNames
            ) {
                if(nameToNums.containsKey(p.getProduceName())){
                    nameToNums.get(p.getProduceName()).add(p.getProduceNum());
                }else{
                    List<String> nums=new ArrayList<>();
                    nums.add(p.getProduceNum());
                    nameToNums.put(p.getProduceName(),nums);
                }
            }

            if (!nameToNums.isEmpty()){
                nameToNums.forEach((key,value)->{
                    System.out.println("分类后的产品名称与对应的nums：" +key+" "+value.toString());
                    /**
                     * 处理获得结果数据
                     */
                    TopResp topResp=new TopResp();
                    ProduceStatisticalInfo produceStatisticalInfos=topStratisProsDao.getStatisnInfo(value);
                    if(produceStatisticalInfos!=null){
                        System.out.println(value.toString()+"  获得的统计信息："+produceStatisticalInfos.toString());
                        topResp.setStatisticalInfo(produceStatisticalInfos);
                        //1.0获取小表的id
                        List<DefectTypeInfo> describeId=topStratisProsDao.getDescribeId(value);
                        List<Object> describe=new ArrayList<>();
                        defectStatisticsService.getDiscrap(describe,describeId);
                        for (Object o: describe
                             ) {
                            System.out.println("各个小表的数据信息："+o.toString());
                        }
                        topResp.setDefectDescribes(describe);
                        respMap.put(key,topResp);
                    }
                });
            }
        }
        return JSON.toJSONString(respMap);
    }
}
